package com.itheima.practice;
// TreeSet案例
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {
    public static void main(String[] args) {
        //创建TreeSet集合对象
        TreeSet<Student> set = new TreeSet<Student>((o1,o2)->{
            int result = o1.getSum()-o2.getSum();
            result = result == 0? o1.getChinese()-o2.getChinese():result;
            result = result == 0? o1.getMath()-o2.getMath():result;
            result = result == 0? o1.getName().compareTo(o2.getName()):result;
            return result;
        });
        //创建学生对象
        Student student1 = new Student("八戒", 80, 90, 100);
        Student student2 = new Student("悟空", 98, 76, 90);
        Student student3 = new Student("三藏", 80, 89, 97);
        Student student4 = new Student("沙和尚", 94, 88, 90);
        Student student5 =  new Student("白骨精", 90, 90, 100);
        //将学生对象添加到集合中
        set.add(student1);
        set.add(student2);
        set.add(student3);
        set.add(student4);
        set.add(student5);

        System.out.println(set);
        System.out.println("===============================");
        //用迭代器遍历
        Iterator<Student> iterator = set.iterator();
        while (iterator.hasNext()){
            Student next = iterator.next();
            System.out.println(next);
        }
        System.out.println("===============================");
        //用增强For遍历
        for (Student student : set) {
            System.out.println(student);
        }
        System.out.println("===============================");

        //用foreach遍历
        set.forEach((student)->{
            System.out.println(student);
        });

    }

}
